using System;
using Amazon.ECS.Model;
using domain = RomanJendrusz.Amazon.ETL.Domain;
using System.Collections.Generic;
using System.Linq;
using BrowseNode=RomanJendrusz.Amazon.ETL.Domain.BrowseNode;

namespace RomanJendrusz.Amazon.ETL.Messages.MessageMappers
{
    public class GetBrowseNodesForProductMapper:  MessageMapper<GetBrowseNodesForProductRequest, ItemLookupRequest, ItemLookupResponse>
    {
        private List<BrowseNode> nodesMap;

        public override ItemLookupRequest MapToAwsRequest(GetBrowseNodesForProductRequest request)
        {
            var awsRequest = new ItemLookupRequest { MerchantId = request.MerchantId };
            awsRequest.ItemId.Add(request.Asin);
            //awsRequest.IdType = "ISBN";
            //awsRequest.SearchIndex = "Books";
            awsRequest.ResponseGroup.Add(request.ResponseGroup);
            awsRequest.MerchantId = request.MerchantId;
            return awsRequest;
        }

        public override AmazonResponse MapToAmazonResponse(ItemLookupResponse awsResponse)
        {
            nodesMap = new List<BrowseNode>();
            Item requestItem = GetRequestItem(awsResponse,0);
            IList<BrowseNode> browseNodes  = new List<BrowseNode>();
            if(requestItem.IsSetBrowseNodes() && requestItem.BrowseNodes.IsSetBrowseNode())
            {
                foreach(var node in requestItem.BrowseNodes.BrowseNode)
                {
                    int nodeId = Convert.ToInt32(node.BrowseNodeId);
                    BrowseNode currentNode = nodesMap.FirstOrDefault(n=>n.BrowseNodeId == nodeId);
                    if(currentNode == null )
                    {
                        currentNode = new BrowseNode(nodeId, node.Name) {IsCategoryRoot = node.IsCategoryRoot};
                        browseNodes.Add(currentNode);
                        nodesMap.Add(currentNode);
                    }
                    else
                    {
                        browseNodes.Add(currentNode);
                        continue;
                    }
                    mapParrentNodes(node, currentNode);
                }
            }
            return new GetBrowseNodesForProductResponse(CorrelationId, browseNodes);
        }

        private void mapParrentNodes(global::Amazon.ECS.Model.BrowseNode node, BrowseNode currentNode)
        {
            int nodeId;
            var amazonsCurrentNode = node.Ancestors;
            bool hasFounParent = false;
            while((amazonsCurrentNode != null && amazonsCurrentNode.BrowseNode[0] != null) && hasFounParent == false)
            {
                global::Amazon.ECS.Model.BrowseNode ancestorNode = amazonsCurrentNode.BrowseNode[0];
                amazonsCurrentNode = ancestorNode.Ancestors;
                nodeId = Convert.ToInt32(ancestorNode.BrowseNodeId);
                var currentParentNode = nodesMap.FirstOrDefault(n => n.BrowseNodeId == nodeId);
                if(currentParentNode == null)
                {
                    currentParentNode = new BrowseNode(nodeId, ancestorNode.Name) { IsCategoryRoot = ancestorNode.IsCategoryRoot };
                    currentNode.ParentNode = currentParentNode;
                    currentNode = currentNode.ParentNode;
                    nodesMap.Add(currentParentNode);
                }
                else
                {
                    currentNode.ParentNode = currentParentNode;
                    hasFounParent = true;
                }
            }
        }
    }
}